//
//  WelcomeViewControll.swift
//  weibo
//
//  Created by 陈伟 on 15/10/18.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import UIKit

class WelcomeViewControll: UIViewController {
    
    //背景图片imageView
    lazy var welcomeView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    lazy var iconView: UIImageView = {
        
            var iv: UIImageView = UIImageView()
        
            iv.sd_setImageWithURL(NSURL(string: (UserAccountViewModel.sharedUserAccount.userAccount?.avatar_large) ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            iv.layer.cornerRadius = 45
            iv.layer.masksToBounds = true
        
        return iv
    }()
    
    lazy var nameLabel: UILabel = {
        let userName = UserAccountViewModel.sharedUserAccount.userAccount?.screen_name ?? ""
        let label = UILabel(title: "欢迎回来,"+userName, fontsize: 18)
        return label
    }()
    
    //头像动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        iconView.snp_updateConstraints { (make) -> Void in
//            make.bottom.equalTo(self.view.snp_bottom).offset(-view.bounds.height + 200)
            make.centerY.equalTo(self.view.snp_centerY).offset(-200)
        }
        
        nameLabel.alpha = 0
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: { () -> Void in
 
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1, delay: 0, options: [], animations: { () -> Void in
                    self.nameLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //欢迎界面完成后 发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: "欢迎界面发来通知")
                })
        }

        
    }
//    lazy var nameLabel: UILabel = {
//        var welcomeLabel = UILabel()
////        welcomeLabel.text = "欢迎回来"
//        welcomeLabel.sizeToFit()
//        return welcomeLabel
//    }()

    override func loadView() {
        super.loadView()
        view = welcomeView
        setupUI()
    }
    override func viewDidLoad() {
        
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
    }
    func setupUI() {
        
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
//            make.bottom.equalTo(self.view.snp_bottom).offset(-200)
            make.centerY.equalTo(self.view.snp_centerY).offset(100)
            make.width.equalTo(85)
            make.height.equalTo(85)
        }
//        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
        }
    }
}
